// app.js
const util = require('/utils/util')
App({
  API: 'https://api.fzudog.cn',
  //   API:'https://api.fzudog.cn',
  merchantAppId: 'wx3a8d93e573d6c286',
  onLaunch() {

  },

  async $upload(path) {

    const baseApi = this.API
    const img = await wx.chooseImage({
      count: 1,
      sizeType: ['original','compressed'],
      sourceType: ['album', 'camera']
    })
    wx.showLoading({ title: '上传中' })

    const file = img.tempFilePaths[0]


    return new Promise((resolve, reject) => {
      wx.uploadFile({
        filePath: file,
        formData: { 'path': path },
        name: 'uploadFile',
        header: {
          'content-type': 'multipart/form-data',
          'path': path
        },
        url: baseApi + '/open/upload',
        success(res) {
          wx.hideLoading()
          resolve(JSON.parse(res.data))
        }
      })
    })



  },

  getNowTime() {
    return util.formatTime(new Date())
  },
  async $request(url, method, data, header) {
    const _this = this
    const baseApi = this.API
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseApi + url,
        method: method || 'POST',
        data: data || {},
        header: header || {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token') || '',
        },
        success: function (res) {

          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },
  globalData: {
    userInfo: null
  }
})
